package com.lh0811.forge.dependency.basic_web.valid;


import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.support.CronExpression;

public class CronValidator implements ConstraintValidator<ValidCron, String> {

    @Override
    public void initialize(ValidCron constraintAnnotation) {
        // 初始化方法，通常不需要做什么  
    }

    @Override
    public boolean isValid(String cronExpression, ConstraintValidatorContext context) {
        try {
            if (StringUtils.isNotBlank(cronExpression)) {
                return CronExpression.isValidExpression(cronExpression); // 尝试解析cron表达式
            }
            return true; // 如果没有抛出异常，说明格式正确
        } catch (Exception e) {
            return false; // 抛出异常，说明格式不正确  
        }
    }
}